#include <config.h>
#include <stdio.h>
#include <ui_manager.h>
#include <page_manager.h>
#include <math.h>
#include <string.h>
#define X_GAP 5
#define Y_GAP 5

static Button g_Buttons[ITEMCFG_MAX_COUNT];
static int g_iButton_Count;

static int MainPageOnPressed(struct Button *ptButton , PDispBuffer ptDispBuffer , PInputEvent ptInputEvent)
{
	unsigned int dwColor = BUTTON_DEFAULT_COLOR;
	char * strButton;
	char name[100];
	char status[100];
	strButton = ptButton->name;
	if(ptInputEvent->iType == INPUT_TYPE_TOUCH)	
	{
		if(GetItemConfigBy_name(ptButton->name)->bCanBeTouched == 0)
			return -1;
		if(ptInputEvent->iPressure)
			ptButton->status = !ptButton->status;
		if(ptButton->status)
			dwColor = BUTTON_PRESSED_COLOR;
	}
	else if(ptInputEvent->iType == INPUT_TYPE_NET)
	{
		sscanf(ptInputEvent->str , "%s %s" , name , status);
		if(strcmp(status , "ok") == 0)
			dwColor = BUTTON_PRESSED_COLOR;
		else if(strcmp(status , "err") == 0)
			dwColor = BUTTON_DEFAULT_COLOR;
		else if(status[0] >= '0' && status[0] <= '9')
		{
			dwColor = BUTTON_PERCENT_COLOR;
			strButton = status;
		}
		else 
			return -1;
	}
	else
	{
		return -1;
	}

	
	DrawRegion(&ptButton->tButtonRegion , dwColor);
	
	DrawTextInRegion_Central(strButton , &ptButton->tButtonRegion , BUTTON_TEXT_COLOR);
	
	FlushDispRegion(&ptButton->tButtonRegion, ptDispBuffer);
	return 0;
}

static int GetFontSizeForAllButton(void)
{
	int i;
	int len;
	int max_len = -1;
	int max_index = 0;
	float kx , ky ,k;
	RegionCartesian tRegionCartesian;
	for(i = 0; i < g_iButton_Count ; i++)
	{
		len = strlen(g_Buttons[i].name);
		if(len > max_len)
		{
			max_len = len;
			max_index = i;
		}	
	}
	Font_SetSize(100);
	Gets_StringRegionCar(g_Buttons[max_index].name , &tRegionCartesian);

	kx = (float)g_Buttons[max_index].tButtonRegion.iWidth / tRegionCartesian.iWidth;
	ky = (float)g_Buttons[max_index].tButtonRegion.iHeigh / tRegionCartesian.iHeigh;
	if(kx < ky)
		k = kx;
	else
		k = ky;
	return k * 100 * 0.8;
	
}

static void GenerartButtons(void)
{
	int heigh , width;
	int rows , row;
	int col;
	int n_per_line;
	int n;
	int Xres , Yres;
	int start_x , start_y;
	int pre_start_x , pre_start_y;
	PDispBuffer pDispBuffer;
	PButton ptButton;
	int i = 0;
	int iFontSize;
	g_iButton_Count = n = ItemCfgCount();
	
	pDispBuffer = Get_DispBuffer();
	Xres = pDispBuffer->iXres;	
	Yres = pDispBuffer->iYres;
	
	width = sqrt(1.0/0.618 * Xres * Yres / n);
	n_per_line = Xres / width + 1;
	width = Xres / n_per_line;
	heigh =  0.618 * width;

	start_x = (Xres - width * n_per_line)/2;

	rows    = n / n_per_line;
	if(rows * n_per_line < n)
		rows++;

	start_y = (Yres - rows*heigh)/2;

	for(row = 0 ; (row < rows) && (i < n) ; row++)
	{
		pre_start_y = start_y + row * heigh;
		pre_start_x = start_x - width;

		for(col=0 ; (col < n_per_line) && (i < n) ; col++)
		{
			ptButton = &g_Buttons[i];
			ptButton->tButtonRegion.iLeftUpx = pre_start_x + width;
			ptButton->tButtonRegion.iLeftUpY = pre_start_y;
			ptButton->tButtonRegion.iWidth   = width - X_GAP;			
			ptButton->tButtonRegion.iHeigh   = heigh - Y_GAP;
			pre_start_x = ptButton->tButtonRegion.iLeftUpx;

			InitButton(ptButton, GetItemConfigBy_index(i)->name, NULL , NULL, MainPageOnPressed);
			i++;
		}
	}
	iFontSize = GetFontSizeForAllButton();
	for(i = 0 ; i<n ; i++)
	{
		g_Buttons[i].iFontSize = iFontSize;
		g_Buttons[i].OnDraw(&g_Buttons[i] , pDispBuffer);	
	}
	
}

static int isTouchPointInRegion(int X ,int Y , PRegion ptRegion)
{
	if(X < ptRegion->iLeftUpx || X > (ptRegion->iLeftUpx + ptRegion->iWidth))
		return 0;
	if(Y < ptRegion->iLeftUpY || Y > (ptRegion->iLeftUpY + ptRegion->iHeigh))
		return 0;
	return 1;
}

static PButton GetButtonBy_name(char *name)
{
	int i;
	for(i = 0; i<g_iButton_Count ; i++)
	{
		if(strcmp(name , g_Buttons[i].name) == 0)
			return &g_Buttons[i];
	}
	
	return NULL;
}
static PButton GetButtonBy_InPutEvent(PInputEvent ptInputEvent)
{
	int i;
	char name[100];
	if(ptInputEvent->iType == INPUT_TYPE_TOUCH)
	{
		for(i = 0 ; i<g_iButton_Count ; i++)
		{
			if(isTouchPointInRegion(ptInputEvent->iX , ptInputEvent->iY , &g_Buttons[i].tButtonRegion))
				return &g_Buttons[i];
		}
	}
	else if(ptInputEvent->iType == INPUT_TYPE_NET)
	{
		sscanf( ptInputEvent->str , "%s" , name);
		return GetButtonBy_name(name);
	}
	else
	{
		return NULL;
	}
	
	return NULL;

}


static void MainPageRun(void * pParams)
{
	InputEvent tInputEvent;
	PButton ptButton;
	int err;
	PDispBuffer ptDispBuffer = Get_DispBuffer();
	
	err = ParseConfigFile();
	if(err)
		return;

	GenerartButtons();
	while(1)
	{
		err = GetInputEvent(&tInputEvent);
		if(!err)
			continue;
		ptButton = GetButtonBy_InPutEvent(&tInputEvent);
		if(!ptButton)
			continue;
		ptButton->OnPressed(ptButton , ptDispBuffer , &tInputEvent);
	}
}

struct PageAction g_tMainPage = {
	.name = "main",
	.Run  = MainPageRun,
};
void MainRegister(void)
{
	PageRegister(&g_tMainPage);
}
